package com.zr.useredu.service.impl;

import com.zr.useredu.mapper.UserEduMapper;
import com.zr.useredu.pojo.UserEdu;
import com.zr.useredu.pojo.UserEduAddVo;
import com.zr.useredu.service.UserEduService;
import com.zr.util.ResultBuildVo;
import com.zr.util.ResultVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * Created by Administrator on 2019/7/3.
 */
@Service
public class UserEduServiceImpl implements UserEduService{
    @Autowired
    private UserEduMapper userEduMapper;

    @Override
    public ResultVo addEducation(UserEduAddVo userEduAddVo,HttpServletRequest request) {
        Integer loginId = (Integer) request.getSession().getAttribute("loginId");
         if(loginId == null){
            return ResultBuildVo.error("请登录","500");
        }else{
             UserEduAddVo userEduVo = userEduMapper.queryLoginIdEdu(loginId);
             if( userEduVo != null ){
                 UserEdu userEdu = new UserEdu();
                 Date nowDate = new Date();
                 userEdu.setUpdateTime(nowDate);
                 userEdu.setUpdateName("张三");
                 //把左边的数据赋值给右边的数据，前提：必须具有相同的参数名称和类型
                 BeanUtils.copyProperties(userEduAddVo,userEdu);
                 userEduMapper.updateEducation(userEdu);
                 return ResultBuildVo.success(userEduAddVo);
             }else{
                 UserEdu userEdu = new UserEdu();
                 Date nowDate = new Date();
                 userEdu.setUpdateTime(nowDate);
                 userEdu.setUpdateName("张三");
                 //把左边的数据赋值给右边的数据，前提：必须具有相同的参数名称和类型
                 BeanUtils.copyProperties(userEduAddVo,userEdu);
                 userEduMapper.addEducation(userEdu);
                 return ResultBuildVo.success(userEduAddVo);
             }
         }


    }

    @Override
    public ResultVo queryLoginIdEdu(@RequestParam("loginId") Integer loginId) {
        if (loginId == null){
            return ResultBuildVo.error("没有登录请重新登录","500");
        }
        userEduMapper.queryLoginIdEdu(loginId);
        return ResultBuildVo.success(loginId);
    }
}
